% Generate figures and and fix font size
%
% Generates some of the figures of the PPFT article using the MAT files
% stored in the current directory.
% Loads FIG files from ../figs/ and fix their font size to the value
% specified in fontSize.
% All output figures (the generated figures and the fixed figures with the
% new font size) are saved in the current directory. They should be copied
% manually into ../figs.
%
% Shlomi Golubev, 2014.
% Revised Yoel Shkolnisky, February 2014.

function readFiguresFromFiles()

fontSize = 14;
FIGDIR=['latex' filesep 'article' filesep 'figs' filesep];

% Create EPS files of the psuedo-polar grid from the FIG files.
fn='fig2dx';    hgload([FIGDIR fn '.fig']); save_pic(fn)
fn='fig2dy';    hgload([FIGDIR fn '.fig']); save_pic(fn)
fn='fig2dxy';   hgload([FIGDIR fn '.fig']); save_pic(fn)
fn='fig3dx';    hgload([FIGDIR fn '.fig']); save_pic(fn)
fn='fig3dy';    hgload([FIGDIR fn '.fig']); save_pic(fn)
fn='fig3dz';    hgload([FIGDIR fn '.fig']); save_pic(fn)
fn='fig3dxyz';  hgload([FIGDIR fn '.fig']); save_pic(fn)

% Create EPS for Jacobian illustrations
fn='trapezoid'; hgload([FIGDIR fn '.fig']); save_pic(fn)
fn='frustum';   hgload([FIGDIR fn '.fig']); save_pic(fn)
fn='calc_trapezoid_area';   hgload([FIGDIR fn '.fig']); save_pic(fn)


fn='2D_error_without_precond';
hgload([FIGDIR fn '.fig']);
set(gcf, 'Position', [100 100 800 800]);
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca,'FontSize', fontSize);
set(gca, 'XLim', [50 2050]);
set(gca, 'YLim', [1.0e-16 15.0e-15]);
set(gca, 'YTick', 0:3.0e-15:15.0e-15);
save_pic(fn)

fn='2D_error_with_precond';
hgload([FIGDIR fn '.fig']);
set(gcf, 'Position', [100 100 800 800]);
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca,'FontSize', fontSize);
set(gca, 'XLim', [50 2050]);
set(gca, 'YLim', [1.0e-16 5e-14]);
set(gca, 'YTick', 0:1.0e-14:5.0e-14);
save_pic(fn)

fn='3D_error_without_precond';
hgload([FIGDIR fn '.fig']);
set(gcf, 'Position', [100 100 800 800]);
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca,'FontSize', fontSize);
set(gca, 'XLim', [50 180]);
set(gca, 'YLim', [1.0e-16 1.0e-13]);
set(gca, 'YTick', 0:0.2e-13:1.0e-13);
save_pic(fn)

fn='3D_error_with_precond';
hgload([FIGDIR fn '.fig']);
set(gcf, 'Position', [100 100 800 800]);
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca,'FontSize', fontSize);
set(gca, 'XLim', [50 180]);
set(gca, 'YLim', [1.0e-16 5.0e-13]);
set(gca, 'YTick', 0:0.1e-12:5.0e-13);
save_pic(fn)

fn='2D_speedup_without_precond';
hgload([FIGDIR fn '.fig']);
set(gcf, 'Position', [100 100 800 800]);
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca,'FontSize', fontSize);
set(gca, 'XLim', [50 2050]);
set(gca, 'YLim', [1 80]);
set(gca, 'YTick', 0:10:80);
save_pic(fn)

fn='2D_speedup_with_precond';
hgload([FIGDIR fn '.fig']);
set(gcf, 'Position', [100 100 800 800]);
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca,'FontSize', fontSize);
set(gca, 'XLim', [50 2050]);
set(gca, 'YLim', [1 80]);
set(gca, 'YTick', 0:10:80);
save_pic(fn)

fn='3D_speedup_without_precond';
hgload([FIGDIR fn '.fig']);
set(gcf, 'Position', [100 100 800 800]);
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca,'FontSize', fontSize);
set(gca, 'XLim', [50 180]);
set(gca, 'YLim', [1 25]);
set(gca, 'YTick', 0:5:25);
save_pic(fn)

fn='3D_speedup_with_precond';
hgload([FIGDIR fn '.fig']);
set(gcf, 'Position', [100 100 800 800]);
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca,'FontSize', fontSize);
set(gca, 'XLim', [50 180]);
set(gca, 'YLim', [1 25]);
set(gca, 'YTick', 0:5:25);
save_pic(fn)

clear n1 n2
load('hist2D.mat');

figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
bar(n1/sum(n1));
set(gca, 'XLim', [0 150]);
set(gca, 'YLim', [1.0e-16 0.16]);
fn='2Dspectrum_no_precond';
save_pic(fn);

figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
bar(n2/sum(n2));
set(gca, 'XLim', [00 150]);
set(gca, 'YLim', [1.0e-16 0.9]);
fn='2Dspectrum_with_precond';
save_pic(fn);

clear n1 n2
load('hist3D.mat')

figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
bar(n1/sum(n1));
set(gca, 'XLim', [0 400]);
set(gca, 'YLim', [1.0e-16 0.15]);
fn='3Dspectrum_no_precond';
save_pic(fn);

figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
bar(n2/sum(n2));
set(gca, 'XLim', [0 400]);
set(gca, 'YLim', [1.0e-16 0.5]);
fn='3Dspectrum_with_precond';
save_pic(fn);

clear result_list
load('precond_results_2D_no_svd.mat')

iter_no_precond=result_list(1,:);
iter_with_precond=result_list(2,:);
condnum_no_precond=result_list(3,:);
condnum_with_precond=result_list(4,:);
vec_size=result_list(7,:);

figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,iter_no_precond)
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [50 2050]);
set(gca, 'YLim', [1.0e-16 350]);
fn='2DnoprecondCGIter';
save_pic(fn);
figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,iter_with_precond)
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [50 2050]);
set(gca, 'YLim', [1.0e-16 20]);
fn='2DwithprecondCGIter';
save_pic(fn);
figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,condnum_no_precond)
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [50 2050]);
set(gca, 'YLim', [1.0e-16 8e+3]);
set(gca, 'YTick', 0:2e+3:8e+3);
fn='2DnoprecondNormIter';
save_pic(fn);
figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,condnum_with_precond)
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [50 2050]);
set(gca, 'YLim', [1.0e-16 6]);
fn='2DwithprecondNormIter';
save_pic(fn);

clear result_list
load('precond_results_2D_with_svd.mat')
condnum_no_precond=result_list(3,:);
condnum_with_precond=result_list(4,:);
svdcondnum_no_precond=result_list(5,:);
svdcondnum_with_precond=result_list(6,:);
vec_size=result_list(7,:);

figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,svdcondnum_no_precond,'-b')
hold on;
plot(vec_size,condnum_no_precond,'--r')
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [0 32]);
set(gca, 'YLim', [1.0e-16 70]);
fn='2DnoprecondSVD';
save_pic(fn);
figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,svdcondnum_with_precond,'-b')
hold on;
plot(vec_size,condnum_with_precond,'--r')
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [0 32]);
set(gca, 'YLim', [1.0e-16 3.5]);
fn='2DwithprecondSVD';
save_pic(fn);

clear result_list
load('precond_results_3D_no_svd.mat')

iter_no_precond=result_list(1,:);
iter_with_precond=result_list(2,:);
condnum_no_precond=result_list(3,:);
condnum_with_precond=result_list(4,:);
vec_size=result_list(7,:);

figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,iter_no_precond)
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [50 180]);
set(gca, 'YLim', [1.0e-16 1000]);
fn='3DnoprecondCGIter';
save_pic(fn);
figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,iter_with_precond)
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [50 180]);
set(gca, 'YLim', [1.0e-16 20]);
fn='3DwithprecondCGIter';
save_pic(fn);
figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,condnum_no_precond)
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [50 180]);
set(gca, 'YLim', [1.0e-16 1.2e+5]);
fn='3DnoprecondNormIter';
save_pic(fn);
figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,condnum_with_precond)
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [50 180]);
set(gca, 'YLim', [1.0e-16 12]);
fn='3DwithprecondNormIter';
save_pic(fn);

clear result_list
load('precond_results_3D_with_svd.mat')
condnum_no_precond=result_list(3,:);
condnum_with_precond=result_list(4,:);
svdcondnum_no_precond=result_list(5,:);
svdcondnum_with_precond=result_list(6,:);
vec_size=result_list(7,:);

figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,svdcondnum_no_precond,'-b')
hold on;
plot(vec_size,condnum_no_precond,'--r')
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [0 16]);
set(gca, 'YLim', [1.0e-16 8e+2]);
set(gca, 'YTick', 0:2e+2:8e+2);
fn='3DnoprecondSVD';
save_pic(fn);
figure
set(gcf, 'Position', [100 100 800 800]);
set(gca,'FontSize', fontSize);
plot(vec_size,svdcondnum_with_precond,'-b')
hold on;
plot(vec_size,condnum_with_precond,'--r')
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca, 'XLim', [0 16]);
set(gca, 'YLim', [1.0e-16 8]);
fn='3DwithprecondSVD';
save_pic(fn);

fn='precond2D';
hgload([FIGDIR fn '.fig']);
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca,'FontSize', fontSize);
set(get(gca,'XLabel'),'FontSize',20)
set(get(gca,'XLabel'),'Interpreter','Latex');
set(get(gca,'XLabel'),'String','$k$');
save_pic(fn)

fn='precond3D';
hgload([FIGDIR fn '.fig']);
set(findobj(gca,'type','line'), 'LineWidth', 2);
set(gca,'FontSize', fontSize);
set(get(gca,'XLabel'),'FontSize',20)
set(get(gca,'XLabel'),'Interpreter','Latex');
set(get(gca,'XLabel'),'String','$k$');
save_pic(fn)

end

